package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.service.BranchCourtsService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@CrossOrigin
public class BranchCourtsController  {

    @Reference
    private BranchCourtsService pagingService;

    @GetMapping("/findAll")
    public Result findPage(){
        try {
            List<Address> addresses = pagingService.pageQuery();
            return new Result(true, MessageConstant.QUERY_BRANCHCOURTS_SUCCESS,addresses);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_BRANCHCOURTS_FAIL);
        }
    }
    @PostMapping("/submit")
    public Result addBranchCourts(@RequestBody Address address){
        try {
            boolean result = pagingService.addBranchCourts(address);
            if(result){
                return new Result(true, MessageConstant.ADD_BRANCHCOURTS_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_BRANCHCOURTS_FAIL);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        try {
            Address address = pagingService.findById(id);
            return new Result(true,MessageConstant.QUERY_BRANCHCOURTS_SUCCESS,address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_BRANCHCOURTS_FAIL);
        }
    }

    @PutMapping("/update")
    public Result updateBranchCourts(@RequestBody Address address){
        try {
            boolean result = pagingService.updateBranchCourts(address);
            if(result){
                return new Result(true, MessageConstant.EDIT_BRANCHCOURTS_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_BRANCHCOURTS_FAIL);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        try {
            pagingService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_BRANCHCOURTS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_BRANCHCOURTS_FAIL);
        }
    }

    @PutMapping("/updateByEnable")
    public Result updateEnableById(Integer id,Integer enable){
        try {
            pagingService.updateEnableById(id,enable);
            return new Result(true, MessageConstant.EDIT_BRANCHCOURTS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_BRANCHCOURTS_FAIL);
        }
    }
}
